package org.zhq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.enums.CommentLevel;
import org.zhq.enums.YesOrNo;
import org.zhq.mapper.*;
import org.zhq.pojo.*;
import org.zhq.pojo.vo.CommentLevelCountsVO;
import org.zhq.pojo.vo.ItemCommentVO;
import org.zhq.pojo.vo.SearchItemsVO;
import org.zhq.service.ItemsService;
import org.zhq.utils.DesensitizationUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemsServiceImpl implements ItemsService {
    private final ItemsMapper itemsMapper;
    private final ItemsImgMapper itemsImgMapper;
    private final ItemsSpecMapper itemsSpecMapper;
    private final ItemsParamMapper itemsParamMapper;
    private final ItemsCommentMapper itemsCommentMapper;

    @Autowired
    public ItemsServiceImpl(ItemsMapper itemsMapper, ItemsImgMapper itemsImgMapper, ItemsSpecMapper itemsSpecMapper, ItemsParamMapper itemsParamMapper, ItemsCommentMapper itemsCommentMapper) {
        this.itemsMapper = itemsMapper;
        this.itemsImgMapper = itemsImgMapper;
        this.itemsSpecMapper = itemsSpecMapper;
        this.itemsParamMapper = itemsParamMapper;
        this.itemsCommentMapper = itemsCommentMapper;
    }

    /**
     * 根据商品id查询详情
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Items queryItemsById(String itemId) {
        return itemsMapper.selectById(itemId);
    }

    /**
     * 根据商品id查询商品图片列表
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsImg> queryItemsImgList(String itemId) {
        LambdaQueryWrapper<ItemsImg> condition = Wrappers.<ItemsImg>lambdaQuery().eq(ItemsImg::getItemId, itemId);
        return itemsImgMapper.selectList(condition);
    }

    /**
     * 根据商品id查询商品规格
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> queryItemsSpecList(String itemId) {
        LambdaQueryWrapper<ItemsSpec> condition = Wrappers.<ItemsSpec>lambdaQuery().eq(ItemsSpec::getItemId, itemId);
        return itemsSpecMapper.selectList(condition);
    }

    /**
     * 根据商品id查询商品参数
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsParam queryItemsParam(String itemId) {
        LambdaQueryWrapper<ItemsParam> condition = Wrappers.<ItemsParam>lambdaQuery().eq(ItemsParam::getItemId, itemId);
        return itemsParamMapper.selectOne(condition);
    }

    /**
     * 根据商品id查询商品的评价等级数量
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CommentLevelCountsVO queryCommentCounts(String itemId) {
        Integer goodCount = queryCommentCounts(itemId, CommentLevel.GOOD.type);
        Integer normalCount = queryCommentCounts(itemId, CommentLevel.NORMAL.type);
        Integer badCount = queryCommentCounts(itemId, CommentLevel.BAD.type);
        int totalCount = goodCount + normalCount + badCount;
        CommentLevelCountsVO commentLevelCountsVO = new CommentLevelCountsVO();
        commentLevelCountsVO.setGoodCounts(goodCount);
        commentLevelCountsVO.setNormalCounts(normalCount);
        commentLevelCountsVO.setBadCounts(badCount);
        commentLevelCountsVO.setTotalCounts(totalCount);
        return commentLevelCountsVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IPage<ItemCommentVO> queryPagedComments(String itemId, Integer level, Integer pageNo, Integer pageSize) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("itemId", itemId);
        paramMap.put("level", level);
        Page page = new Page(pageNo, pageSize);
        IPage<ItemCommentVO> itemComments = itemsCommentMapper.queryItemComments(page, paramMap);
        for (ItemCommentVO record : itemComments.getRecords()) {
            record.setNickname(DesensitizationUtil.commonDisplay(record.getNickname()));
        }
        return itemComments;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IPage<SearchItemsVO> searchItems(String keywords, String sort, Integer pageNo, Integer pageSize) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("keywords", keywords);
        paramMap.put("sort", sort);
        Page page = new Page(pageNo, pageSize);
        IPage<SearchItemsVO> pageResult = itemsMapper.searchItems(page, paramMap);
        return pageResult;
    }


    /**
     * 根据分类id 排序方式 分页信息 查询商品列表
     *
     * @param catId
     * @param sort
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IPage<SearchItemsVO> searchItemsByThirdCat(Integer catId, String sort, Integer pageNo, Integer pageSize) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("catId", catId);
        paramMap.put("sort", sort);
        Page page = new Page(pageNo, pageSize);
        IPage<SearchItemsVO> pageResult = itemsMapper.searchItemsByThirdCat(page, paramMap);
        return pageResult;
    }

    /**
     * 根据规格id 查询商品规格信息
     *
     * @param specIds
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> queryItemSpecListByIds(List<String> specIds) {
        LambdaQueryWrapper<ItemsSpec> condition = Wrappers.<ItemsSpec>lambdaQuery().in(ItemsSpec::getId, specIds);
        List<ItemsSpec> itemsSpecList = itemsSpecMapper.selectList(condition);
        return itemsSpecList;
    }

    /**
     * 根据规格id 查询商品规格信息
     *
     * @param specId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsSpec queryItemSpecById(String specId) {
        return itemsSpecMapper.selectById(specId);
    }

    /**
     * 根据商品id获取商品图片主图
     *
     * @param itemId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public String queryItemMainImgById(String itemId) {
        LambdaQueryWrapper<ItemsImg> condition = Wrappers.<ItemsImg>lambdaQuery().eq(ItemsImg::getItemId, itemId).eq(ItemsImg::getIsMain, YesOrNo.YES.type);
        ItemsImg itemsImg = itemsImgMapper.selectOne(condition);
        return itemsImg != null ? itemsImg.getUrl() : "";
    }

    /**
     * 减少库存
     *
     * @param specId
     * @param buyCounts
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void decreaseItemSpecStock(String specId, int buyCounts) {
        //0. 查询库存
        //1.判断库存,是否能够减掉
        boolean result = itemsMapper.decreaseItemSpecStock(specId, buyCounts);
        if(!result){
            throw new RuntimeException("库存不足,订单创建失败");
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    Integer queryCommentCounts(String itemId, Integer level) {
        LambdaQueryWrapper<ItemsComments> condition = Wrappers.<ItemsComments>lambdaQuery().eq(ItemsComments::getItemId, itemId).eq(level != null, ItemsComments::getCommentLevel, level);
        return itemsCommentMapper.selectCount(condition);
    }

}
